//
//  PopViewAnimator.swift
//  WeiBoSwift
//
//  Created by admin on 2017/12/13.
//  Copyright © 2017年 admin. All rights reserved.
//

import UIKit

class PopViewAnimator: NSObject {
    var isPresented : Bool = false
    var presentedFrame : CGRect = CGRect.zero
}


//MARK:- TransitioningDelegate
extension PopViewAnimator : UIViewControllerTransitioningDelegate {
    // 推出的UIPresentationController
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let presentationC : WFPresentationController = WFPresentationController(presentedViewController: presented, presenting: presenting)
        presentationC.presentFrame = presentedFrame
        return presentationC
    }
    
    // 设置推出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    // 设置消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

//MARK:- UIViewControllerAnimatedTransitioning
extension PopViewAnimator : UIViewControllerAnimatedTransitioning {
    // 返回动画时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    // 动画
    // UITransitionContextFromViewKey 消失动画
    // UITransitionContextToViewKey 显示动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animatePresentView(using: transitionContext) : animateDismissView(using: transitionContext)
    }
    
    // 自定义显示view的动画
    private func animatePresentView(using transitionContext: UIViewControllerContextTransitioning) {
        // 获取显示的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        // 添加到当前的view上
        transitionContext.containerView.addSubview(presentedView)
        // 添加动画
        presentedView.transform = CGAffineTransform(scaleX: 1, y: 0)
        presentedView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform(scaleX: 1, y: 1)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    // 自定义消失view的动画
    private func animateDismissView(using transitionContext: UIViewControllerContextTransitioning) {
        // 获取消失的view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        // 消失动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView.transform = CGAffineTransform(scaleX: 1, y: 0.00001)
        }) { (_) in
            dismissView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}
